from playwright.sync_api import sync_playwright
from datetime import datetime
import time

with sync_playwright() as p:
    try:
        wobser = p.chromium.launch(headless=False)  #关闭无头模式
        page = wobser.new_page()                    #通过浏览器打开新页面
        page.goto('http://10.59.9.12:7001/')                               #通过网址打开a3mall
        #点击右上方登录
        page.locator('xpath=/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
        time.sleep(2)
        #断言1————会员登录
        dy1 = page.locator('xpath=/html/body/div[4]/div/div[1]/div[1]/span[1]').text_content()
        print(dy1)
        time.sleep(3)
        #输入用户名
        page.locator('[name="username"]').fill('13811033962')
        #输入密码
        page.locator('[name="password"]').fill('123456')
        time.sleep(8)
        #点击登录按钮
        page.locator('xpath=/html/body/div[4]/div/div[1]/div[2]/form/div[5]/a').click()
        time.sleep(5)
        #断言2————会员中心
        dy2 = page.locator('xpath=/html/body/div[1]/div/div[2]/ul/li[3]/a').text_content()
        print(dy2)
        time.sleep(3)
        time = datetime.now().strftime('%Y%m%d%H%M%S')  #当前时间戳
        page.screenshot(path=f'photo_{time}.png')
    except Exception as e:
        print(e)
    wobser.close()              #关闭浏览器
